package com.zis.system.server;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zis.starter.core.Constant;
import com.zis.starter.core.ResponseEntity;
import com.zis.starter.core.SearchParams;
import com.zis.system.dao.GroupResDao;
import com.zis.system.inter.IGroupResInter;
import com.zis.system.model.GroupRes;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Create by wangshen 2018/4/17
 */
@Service
@Transactional("txManager")
public class GroupResInterImpl implements IGroupResInter {

    @Autowired
    private GroupResDao groupResDao;

    @Override
    public ResponseEntity insert(GroupRes model) throws Exception {
        return null;
    }

    @Override
    public ResponseEntity update(GroupRes model) throws Exception {
        return null;
    }

    @Override
    public ResponseEntity delete(String id) throws Exception {
        return null;
    }

    @Override
    public ResponseEntity selectById(String id) throws Exception {
        return null;
    }

    @Override
    public PageQuery<GroupRes> findByPage(SearchParams params) throws Exception {
        return null;
    }

    @Override
    public ResponseEntity findByCondition(Map params) throws Exception {
        return null;
    }

    /**
     * 用户组资源保存
     * @param params
     * @return
     * @throws Exception
     */
    @Override
    public ResponseEntity groupResSave(JSONObject params) throws Exception {
        if (!params.containsKey("groupId") || params.getString("groupId").equals("")) {
            return ResponseEntity.failure("用户组信息为空");
        }
        if (!params.containsKey("groupRes") || params.getString("groupRes").equals("")) {
            return ResponseEntity.failure("没有需要设置的用户组资源信息");
        }
        String groupId = params.getString("groupId");
        JSONArray resArray = JSON.parseArray(params.getString("groupRes"));
        if (resArray.size() == 0) {
            return ResponseEntity.failure("没有需要设置的用户组资源信息");
        }
        List<GroupRes> groupResList = new ArrayList<>();
        for (Object obj : resArray) {
            JSONObject resObj = JSON.parseObject(JSON.toJSONString(obj));
            GroupRes groupRes = new GroupRes();
            groupRes.setGroupId(groupId);
            groupRes.setResId(resObj.getString("id"));
            groupRes.setRemarks(resObj.getString("idKey"));
            groupResList.add(groupRes);
        }
        // 删除原有记录
        groupResDao.deleteResByGroupId(groupId);
        // 批量插入数据
        groupResDao.insertBatch(groupResList);
        return ResponseEntity.success(Constant.DATA_RESULT_SUCCEED);
    }

    /**
     * 用户组资源查询
     * @param groupId
     * @return
     * @throws Exception
     */
    @Override
    public ResponseEntity findGroupResTree(String groupId) throws Exception {
        List<GroupRes> groupResList = groupResDao.findByGroupId(groupId);
        if (groupResList!=null && groupResList.size()>0) {
            JSONArray resTree = new JSONArray();
            for (GroupRes groupRes : groupResList) {
                JSONObject resObj = new JSONObject();
                resObj.put("id", groupRes.getResId());
                resObj.put("idKey", groupRes.getRemarks());
                resTree.add(resObj);
            }
            return ResponseEntity.success(resTree);
        }
        return ResponseEntity.failure(Constant.DATA_RESULT_NULL);
    }

    @Override
    public List<GroupRes> findByGroupId(String groupId) throws Exception {
        return groupResDao.findByGroupId(groupId);
    }
}
